﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Text;
using System.Drawing;
using Maticsoft.DBUtility;
using EnvironmentManagerSystem.MODE;
using EnvironmentManagerSystem.BLL;

namespace EnvironmentManagerSystem.HuanTong
{
    public partial class HTUserMain : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (StfInfo.ReSession()) return; if (!IsPostBack)
            {
                DBBingsfsfsd();
                if (Session["ZhiNeng"].ToString() != "")
                {
                    Label1.Text = Session["pjName"].ToString();
                }
                Bind();
            }
        }

        #region DBBingsfsfsd
        public void DBBingsfsfsd()
        {
            bool TZ = Session["ZhiNeng"].ToString().Contains("厅长");
            bool CZ = Session["ZhiNeng"].ToString().Contains("处长");
            bool JBR = Session["ZhiNeng"].ToString().Contains("经办人");
            bool SLR = Session["ZhiNeng"].ToString().Contains("受理人");
            //bool a = Session["ZhiNeng"].ToString().Contains("");
            string aa = Session["ZhiNeng"].ToString();
            int Step = 0;
            if (Request.QueryString["Step"] != string.Empty && Request.QueryString["Step"] != null)
                Step = int.Parse(Request.QueryString["Step"]);
            switch (Step)
            {
                case 0:
                    //HiddenField1.Value = string.Format("and js={0}", int.Parse(Session["userid"].ToString()));
                    break;
                case 1:
                    if (TZ == true || CZ == true || SLR == true)
                        HiddenField1.Value += " and shoulituihui is NULL";
                    if (aa == "厅长" || aa == "处长" || aa == "受理人")
                    {
                        NewMethod1();
                    }
                    else
                    {
                        NewMethod();
                    }
                    //if (a == true) { }
                    break;
                case 2:
                    if (TZ == true || CZ == true)
                        HiddenField1.Value += " and shoulituihui=1 and renyuanfenpeirenyuan is NULL";
                    //if (a == true) { }
                    if (aa == "厅长" || aa == "处长")
                    {
                        NewMethod1();
                    }
                    else
                    {
                        NewMethod();
                    }

                    break;
                case 3:
                    if (JBR == true)
                        HiddenField1.Value += " and shoulituihui=1 and renyuanfenpeirenyuan IS NOT NULL and qicaowenjianrenyuan IS NULL";
                    //if (a == true) { }
                    if (aa == "经办人")
                    {
                        NewMethod1();
                    }
                    else
                    {
                        NewMethod();
                    }

                    break;
                case 4:
                    if (CZ == true)
                        HiddenField1.Value += " and shoulituihui=1 and qicaowenjianrenyuan IS NOT NULL and shangchuantingzrenyuan IS NULL";
                    //if (a == true) { }
                    if (aa == "处长")
                    {
                        NewMethod1();
                    }
                    else
                    {
                        NewMethod();
                    }
                    break;
                case 5:
                    if (TZ == true)
                        HiddenField1.Value += " and shoulituihui=1 and shangchuantingzrenyuan IS NOT NULL and tingzhangqianfaren IS NULL";
                    //if (a == true) { }
                    if (aa == "厅长")
                    {
                        NewMethod1();
                    }
                    else
                    {
                        NewMethod();
                    }
                    break;
            }
            //if (Session["ZhiNeng"].ToString() != "")
            //{
            //    DataTable dt = HuanPingService.GetTabless(HiddenField1.Value + " and ProjectName LIKE '%" + txtsou.Text.Trim() + "%'");
            //    if (dt != null && dt.Rows.Count > 0)
            //    {
            //        Session["pjName"] = dt.Rows[0]["ProjectName"].ToString();
            //        string ahs = Session["pjName"].ToString();
            //        Session["i"] = dt.Rows[0]["ID"].ToString();
            //    }
            //    else
            //    {
            //        Session["pjName"] = "无";
            //        Session["i"] = "无";
            //    }
            //    pjbgdv.DataSource = dt;
            //    pjbgdv.DataBind();
            //}
        }

        private void NewMethod1()
        {
            if (Session["ZhiNeng"].ToString() != "")
            {
                DataTable dt = HuanPingService.GetTabless(HiddenField1.Value + " and ProjectName LIKE '%" + txtsou.Text.Trim() + "%'");
                if (dt != null && dt.Rows.Count > 0)
                {
                    Session["pjName"] = dt.Rows[0]["ProjectName"].ToString();
                    string ahs = Session["pjName"].ToString();
                    Session["i"] = dt.Rows[0]["ID"].ToString();
                }
                else
                {
                    Session["pjName"] = "无";
                    Session["i"] = "无";
                }
                pjbgdv.DataSource = dt;
                pjbgdv.DataBind();
            }
        }

        private void NewMethod()
        {
            if (Session["ZhiNeng"].ToString() != "")
            {
                DataTable dt = HuanPingService.GetTabless(HiddenField1.Value + " and ProjectName = '" + txtsou.Text.Trim() + "'");
                if (dt != null && dt.Rows.Count > 0)
                {
                    Session["pjName"] = dt.Rows[0]["ProjectName"].ToString();
                    string ahs = Session["pjName"].ToString();
                    Session["i"] = dt.Rows[0]["ID"].ToString();
                }
                else
                {
                    Session["pjName"] = "无";
                    Session["i"] = "无";
                }
                pjbgdv.DataSource = dt;
                pjbgdv.DataBind();
            }
        }
        #endregion

        #region GridView
        protected void pjbgdv_PageIndexChanged(object sender, EventArgs e)
        {
            DBBingsfsfsd();
        }
        protected override void Render(HtmlTextWriter writer)
        {
            foreach (GridViewRow r in pjbgdv.Rows)
            {
                if (r.RowType == DataControlRowType.DataRow)
                {
                    r.Cells[1].Attributes["onclick"] = ClientScript.GetPostBackEventReference(this.pjbgdv, "Select$" + r.RowIndex, true);
                    r.Cells[1].Attributes["title"] = "行单击事件";
                }
            }
            base.Render(writer);
        }
        protected void pjbgdv_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            pjbgdv.PageIndex = e.NewPageIndex;
        }
        protected void pjbgdv_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            string ll = pjbgdv.Rows[0].Cells[1].Text;
        }
        #region 根据紧急情况变色(x)
                protected void pjbgdv_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                e.Row.Cells[1].Attributes.Add("onmouseover", "c=this.style.backgroundColor;this.style.backgroundColor='#00A9FF'");
                e.Row.Cells[1].Attributes.Add("onmouseout", "this.style.backgroundColor=c");
                if (((Label)e.Row.FindControl("Label2")).Text == "特急")
                    e.Row.BackColor = Color.Red;
                else if (((Label)e.Row.FindControl("Label2")).Text == "急")
                    e.Row.BackColor = Color.Yellow;
            }
        }
        #endregion
        protected void pjbgdv_SelectedIndexChanging(object sender, GridViewSelectEventArgs e)
        {
            if (pjbgdv.Rows[e.NewSelectedIndex].RowType == DataControlRowType.DataRow)
            {
                Session["pjName"] = ((Label)(pjbgdv.Rows[e.NewSelectedIndex].Cells[1].FindControl("Label1"))).Text.ToString();
                DataTable dt = HuanPingService.GetTabless(HiddenField1.Value + " and ProjectName like '%" + txtsou.Text.Trim() + "%'");
                pjbgdv.DataSource = dt;
                pjbgdv.DataBind();
                Label1.Text = Session["pjName"].ToString();
                Bind();
            }
        }
        #endregion

        #region TextChanged
        protected void txtsou_TextChanged(object sender, EventArgs e)
        {
            DBBingsfsfsd();
        }
        #endregion

        #region 星星显示(x)
        public void Bind()
        {
            try
            {
                if (Session["pjName"] != null && !Session["pjName"].ToString().Equals("") && !Session["pjName"].ToString().Equals("无"))
                {
                    HuanPingDataCollection lst = HuanPingService.GetByProjectName(Session["pjName"].ToString());
                    if (lst.Count > 0)
                    {
                        //受理状态
                        if (lst[0].Shoulituihui.ToString() != "" && lst[0].Shoulituihui == 1)
                        {
                            xiangmushouliren.Text = string.Format("已受理");
                            Image1.ImageUrl = "~/images/bookmark-2.ico";
                            //受理时间 
                            xiangmushoulishijians.Text = lst[0].xiangmushoulishijians.ToString();
                        }
                        else
                        {
                            xiangmushouliren.Text = "未受理";
                            Image1.ImageUrl = "~/images/white-star.png";
                            xiangmushoulishijians.Text = "";
                        }
                        //人员分配状态
                        if (lst[0].Querenfenpei.ToString() != "" && lst[0].Querenfenpei == 1)
                        {
                            fenpeiquerenren.Text = string.Format("已确认");
                            Image2.ImageUrl = "~/images/bookmark-2.ico";
                            //项目分配日期
                            fenpeiquerenshijian.Text = lst[0].Fenpeidate.ToString();
                            //用时几天
                            DateTime DateTime1 = Convert.ToDateTime(lst[0].xiangmushoulishijians.ToString());//起始时间
                            DateTime DateTime2 = Convert.ToDateTime(lst[0].Fenpeidate.ToString());//结束时间                                
                            string dateDiff1 = null;
                            TimeSpan ts1 = new TimeSpan(DateTime1.Ticks);
                            TimeSpan ts2 = new TimeSpan(DateTime2.Ticks);
                            TimeSpan ts3 = ts1.Subtract(ts2).Duration();
                            int ts33 = HuanPingService.ProNoticeDays(lst[0].Fenpeidate.ToString(), lst[0].xiangmushoulishijians.ToString());
                            //显示时间
                            dateDiff1 = "用时" + (ts3.Days + 1 - ts33).ToString() + "天";
                            Time2.Text = dateDiff1;
                            Time2.Visible = true;
                        }
                        else
                        {
                            fenpeiquerenren.Text = "未处理";
                            Image2.ImageUrl = "~/images/white-star.png";
                            fenpeiquerenshijian.Text = "";
                            Time2.Visible = false;
                        }
                        //起草文件状态
                        if (lst[0].Qicaowenjianrenyuan != null && lst[0].Qicaowenjianrenyuan != "" && lst[0].Wenjian == 1)
                        {
                            xiangmufuzequerenren.Text = string.Format("已确认");
                            Image3.ImageUrl = "~/images/bookmark-2.ico";
                            //项目负责日期
                            xiangmufuzequerenriqi.Text = lst[0].Qicaowenjianshijian.ToString();
                            //用时几天
                            DateTime DateTime3 = Convert.ToDateTime(lst[0].xiangmushoulishijians.ToString());//起始时间
                            DateTime DateTime4 = Convert.ToDateTime(lst[0].Qicaowenjianshijian.ToString());//结束时间
                            string dateDiff2 = null;
                            TimeSpan ts4 = new TimeSpan(DateTime3.Ticks);
                            TimeSpan ts5 = new TimeSpan(DateTime4.Ticks);
                            TimeSpan ts6 = ts4.Subtract(ts5).Duration();
                            int ts66 = HuanPingService.ProNoticeDays(lst[0].Qicaowenjianshijian.ToString(), lst[0].xiangmushoulishijians.ToString());
                            //显示时间
                            dateDiff2 = "用时" + (ts6.Days + 1 - ts66).ToString() + "天";
                            Time3.Text = dateDiff2;
                            Time3.Visible = true;
                        }
                        else
                        {
                            xiangmufuzequerenren.Text = "未处理";
                            Image3.ImageUrl = "~/images/white-star.png";
                            xiangmufuzequerenriqi.Text = "";
                            Time3.Visible = false;
                        }
                        //上传厅长状态
                        if (lst[0].Shangchuantingzrenyuan != null && lst[0].Shangchuantingzrenyuan != "" && lst[0].Querenshangchuan == 1)
                        {
                            tachaquerenren.Text = string.Format("已确认");
                            Image4.ImageUrl = "~/images/bookmark-2.ico";
                            //现场踏查日期
                            tachaquerenriqi.Text = lst[0].Shangchuantingzhangshijian.ToString();
                            //用时几天
                            DateTime DateTime5 = Convert.ToDateTime(lst[0].xiangmushoulishijians.ToString());//起始时间
                            DateTime DateTime6 = Convert.ToDateTime(lst[0].Shangchuantingzhangshijian.ToString());//结束时间
                            string dateDiff3 = null;
                            TimeSpan ts7 = new TimeSpan(DateTime5.Ticks);
                            TimeSpan ts8 = new TimeSpan(DateTime6.Ticks);
                            TimeSpan ts9 = ts7.Subtract(ts8).Duration();
                            int ts99 = HuanPingService.ProNoticeDays(lst[0].Shangchuantingzhangshijian.ToString(), lst[0].xiangmushoulishijians.ToString());
                            //显示时间
                            dateDiff3 = "用时" + (ts9.Days + 1 - ts99).ToString() + "天";
                            Time4.Text = dateDiff3;
                            Time4.Visible = true;
                        }
                        else
                        {
                            tachaquerenren.Text = "未处理";
                            Image4.ImageUrl = "~/images/white-star.png";
                            tachaquerenriqi.Text = "";
                            Time4.Visible = false;
                        }
                        //厅长签发状态
                        if (lst[0].Tingzhangqianfaren != null && lst[0].Tingzhangqianfaren != "")
                        {
                            zhuanjiahuiyiquerenren.Text = string.Format("已确认");
                            Image5.ImageUrl = "~/images/bookmark-2.ico";
                            //专家会议时间
                            zhuanjiahuiyiquerenriqi.Text = lst[0].Tingzhangqianfashijian.ToString();
                            //用时几天
                            DateTime DateTime7 = Convert.ToDateTime(lst[0].xiangmushoulishijians.ToString());//起始时间
                            DateTime DateTime8 = Convert.ToDateTime(lst[0].Tingzhangqianfashijian.ToString());//结束时间
                            string dateDiff4 = null;
                            TimeSpan ts10 = new TimeSpan(DateTime7.Ticks);
                            TimeSpan ts11 = new TimeSpan(DateTime8.Ticks);
                            TimeSpan ts12 = ts10.Subtract(ts11).Duration();
                            int ts1212 = HuanPingService.ProNoticeDays(lst[0].Tingzhangqianfashijian.ToString(), lst[0].xiangmushoulishijians.ToString());
                            //显示时间
                            dateDiff4 = "用时" + (ts12.Days + 1 - ts1212).ToString() + "天";
                            Time5.Text = dateDiff4;
                            Time5.Visible = true;
                        }
                        else
                        {
                            zhuanjiahuiyiquerenren.Text = "未处理";
                            Image5.ImageUrl = "~/images/white-star.png";
                            zhuanjiahuiyiquerenriqi.Text = "";
                            Time5.Visible = false;
                        }
                    }
                }
            }
            catch
            { }
        }
        #endregion
    }
}
